package com.webwork.framework.filter;

import java.util.Date;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.webwork.common.dataobject.DateFormat;

public class ShiroLogoutFilter extends LogoutFilter {
	
	private static final Logger log = LoggerFactory.getLogger(ShiroLogoutFilter.class);

	@Override
	protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
		Subject subject = getSubject(request, response);
		String userName = (String) subject.getPrincipal();
		// 登出
		log.info("{}于{}退出系统", userName, DateFormat.Simple.format(new Date()));
		subject.logout();

		// 获取登出后重定向到的地址
		String redirectUrl = getRedirectUrl(request, response, subject);
		// 重定向
		issueRedirect(request, response, redirectUrl);
		return false;
	}

}
